package com.lxc.networkdemo

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.lxc.lib.net.http.HttpManager
import com.lxc.lib.net.http.RequestHelper
import com.lxc.networkdemo.apis.InfoApis
import com.lxc.networkdemo.databinding.ActivityDemo1Binding
import kotlinx.coroutines.GlobalScope

/**
 *@author: xuancai.Liu
 *@create: 2021-09-08 16:28
 *@program: NetWorkDemo
 *@description:
 */
class DemoActivity1 : AppCompatActivity() {
    private val TAG = "DemoActivity1"
    val api = RequestHelper.getApi<InfoApis>(baseUrl = "http://wthrcdn.etouch.cn")

    private lateinit var binding: ActivityDemo1Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityDemo1Binding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.run {
            btn1.setOnClickListener {
                getData()
            }
            btn2.setOnClickListener {
                    finish()
                    getData2()
            }
        }
    }

    private fun getData() {
        HttpManager.Builder(GlobalScope).build().goJson(api.getDataForDeferredAsync("广州")) {
            if (it.status) {
                //success
                it.result?.data?.let { data ->
                    Log.e(TAG, data.toString())
                }
            } else {
                //error
                Log.e(TAG, "error ${it.msg.toString()}")
            }
        }
    }

    private fun getData2() {
        HttpManager.Builder(lifecycleScope).build().goJson(api.getDataForDeferredAsync("上海")) {
            if (it.status) {
                //success
                it.result?.data?.let { data ->
                    Log.e(TAG, data.toString())
                }
            } else {
                //error
                Log.e(TAG, "error ${it.msg.toString()}")
            }
        }
    }

}